﻿using DataModel;
using LinqToDB.DataProvider;
using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using Business.Data;

namespace Common
{
    public class Data
    {
        /// <summary>
        /// 主库
        /// </summary>
        public static DataBase DB = new DataBase("MainOracle");


        public class DataBase : DataBase<Connection>
        {
            static DataBase()
            {
                LinqToDB.Common.Configuration.Linq.AllowMultipleQuery = true;
                LinqToDB.Data.DataConnection.TurnTraceSwitchOn();
                LinqToDB.Data.DataConnection.OnTrace = c =>
                {
                    //var con = c.DataConnection as LinqToDB.LinqToDBConnection;
                    //System.Console.WriteLine($"{con.TraceId}:{con.TraceMethod} {c.SqlText}");
                };
                var config = new ConfigurationBuilder().SetBasePath(AppDomain.CurrentDomain.BaseDirectory).AddJsonFile("appsettings.json").Build();
                LinqToDB.Data.DataConnection.DefaultSettings = new LinqToDB.LinqToDBSection(config.GetSection("AppSettings").GetSection("ConnectionStrings").GetChildren().Select(c => new LinqToDB.ConnectionStringSettings { Name = c.Key, ConnectionString = c.GetValue<string>("ConnectionString"), ProviderName = c.GetValue<string>("ProviderName") }));
                //LinqToDB.Data.DataConnection.DefaultDataProvider = provider.Name;
            }
            readonly string configuration;
            readonly string traceMethod;
            readonly static IDataProvider provider = new LinqToDB.DataProvider.Oracle.OracleDataProvider();
            public DataBase(string configuration = null, string traceMethod = null)
            {
                this.configuration = configuration ?? LinqToDB.Data.DataConnection.DefaultSettings.DefaultConfiguration;
                this.traceMethod = traceMethod;
            }

            public override Connection GetConnection([CallerMemberName] string callMethod = null) => new DataModel.Connection(configuration) { TraceMethod = traceMethod };
        }


        //public class DataBase : Business.Data.DataBase<DataModel.Connection>
        //{
        //    public static DataBase DB = new DataBase();

        //    static DataBase()
        //    {
        //        LinqToDB.Common.Configuration.Linq.AllowMultipleQuery = true;
        //        LinqToDB.Data.DataConnection.TurnTraceSwitchOn();
        //        LinqToDB.Data.DataConnection.OnTrace = c => { };
        //    }

        //    public override DataModel.Connection GetConnection([CallerMemberName] string callMethod = null) => new DataModel.Connection(LinqToDB.Data.DataConnection.DefaultSettings.DefaultConfiguration) { TraceMethod = callMethod };
        //}



    }

    /// <summary>
    /// Redis
    /// </summary>
    //public abstract class Rds : RedisHelper<Rds> { }

    /// <summary>
    /// ELKRedis
    /// </summary>
    //public abstract class ELKRds : RedisHelper<ELKRds> { }
}
